import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/notify/chat_obj.dart';
import 'package:xws/res/icons.dart';
import 'package:xws/state/app_state.dart';
import 'package:xws/ui/notify/chat/start_chat.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_list_tile.dart';

import 'sys_not_items/system_notify.dart';

/// 消息项
class NotifyItem extends StatefulWidget {
  const NotifyItem({
    Key? key,
    required this.chatObj,
    this.isFirst = false,
    this.isLast = false,
    this.refresh,
  }) : super(key: key);

  @override
  _NotifyItemState createState() => _NotifyItemState();

  final ChatObj chatObj;
  final bool isFirst;
  final bool isLast;
  final Function()? refresh;
}

class _NotifyItemState extends State<NotifyItem> with SafeState<NotifyItem> {
  ChatObj get _co => widget.chatObj;

  Future<void> _onTap() async {
    apps.changeUnread(-_co.unreadCount);
    safeSetState(() => _co.unreadCount = 0);

    if (_co.type == RoomType.USER)
      await enterChatRoom(context, _co, unreadCallback: (int c) {
        safeSetState(() => _co.unreadCount = c);
      });
    else
      await push(context, SystemNotify(id: _co.id));

    widget.refresh?.call();
  }

  @override
  Widget build(BuildContext context) {
    double top = 0;
    double bottom = 0;

    if (widget.isFirst || _co.type == RoomType.SYSTEM) top = 10;
    if (widget.isLast || _co.type == RoomType.SYSTEM) bottom = 10;

    Widget child = AppListTile(
      onTap: _onTap,
      borderRadius: BorderRadius.vertical(
        top: Radius.circular(top),
        bottom: Radius.circular(bottom),
      ),
      padding: const EdgeInsets.all(12),
      backgroundColor: white(context),
      leading: Container(
        width: 50,
        height: 50,
        child: _buildHeader,
        alignment: Alignment.center,
        margin: const EdgeInsets.only(right: 10),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: theme(context),
        ),
      ),
      title: Padding(
        padding: const EdgeInsets.only(bottom: 4),
        child: Text(
          _getTitle,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      subtitle: Text(
        _getSubTitle,
        style: TextStyle(fontSize: 12, color: disable(context)),
      ),
      trailing: _buildTrailing,
    );

    if (_co.type == RoomType.SYSTEM) {
      child = Padding(padding: const EdgeInsets.only(bottom: 16), child: child);
    }

    return child;
  }

  /// 构建头像
  Widget get _buildHeader {
    if (_co.type == RoomType.SYSTEM)
      return Icon(
        IR.notify,
        color: white(context),
      );

    return AppImg.fitCover(url: _co.fromUserAvatarUrl, radius: 8);
  }

  /// 构建标题
  String get _getTitle {
    if (_co.type == RoomType.SYSTEM) return '系统通知';

    return _co.fromUserNickName;
  }

  /// 构建副标题
  String get _getSubTitle {
    if (_co.type == RoomType.SYSTEM) return '系统通知会在这里显示';

    return '[物品]' + _co.goodsTitle;
  }

  /// 构建尾部
  Widget get _buildTrailing {
    return _co.unreadCount == 0
        // ? Text(
        //     getDurationDate(_co.updatedAt),
        //     style: const TextStyle(fontSize: 12),
        //   )
        ? const SizedBox.shrink()
        : Container(
            height: 16,
            alignment: Alignment.center,
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: Text(
              _co.unRead,
              style: TextStyle(
                fontSize: 9,
                fontWeight: FontWeight.bold,
                color: white(context),
              ),
            ),
            constraints: const BoxConstraints(minWidth: 16),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(40),
              color: red(context),
            ),
          );
  }
}
